
/*
 *************************************************************************
 *西瑞克斯研发管理系统
 *版 本: 1.0   
 *日 期：02/08/2012    
 *作 者： 王浩 QQ：873987060   
 *************************************************************************   
 */
package com.application.model.struts.actions;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.manager.commons.config.ConfigFactory;
import com.manager.commons.page.PageBean;
import com.manager.commons.RandomKey;
import com.manager.commons.CommonUtil;
import com.application.model.entity.Model;
import com.application.model.struts.form.ModelForm;
import com.application.model.service.ModelService;
import com.application.model.commons.Constants;

/**
 * 
 * Description:ModelAction<br />
 * 
 * Version:V0.1<br />
 * 
 * Author: 王浩 QQ：873987060 <br />
 * 
 * History: <br />
 * 
 * 02/08/2012
 * 
 */
public class ModelAction extends DispatchAction {

	/**
	 * Service层实例
	 */
	private ModelService modelService;

	/**
	 * 日志
	 */
	private Logger log = Logger.getLogger(this.getClass());

	/**
	 * 实体对象
	 */
	private Model model;

	/**
	 * 采用partialList方式分页程序
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward getModelPartialList(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("searchModel" + "开始");
		ModelForm model = (ModelForm) form;
		// 统计总记录数的sql语句
		int resultSize = 0;
		// 初始化pageBean供分页所用
		PageBean pageBean = null;
		// 取得当前页list数据
		List modelList = null;
		resultSize = modelService.getAllCountModel(model);
		pageBean = PageBean.getPageBean("modelTab", resultSize, request);
		modelList = modelService.searchModel(model,
				pageBean);
		request.setAttribute("modelList", modelList);

		log.debug("searchModel" + "结束");

		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 到达新增页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toAddModel(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("toAddModels");
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 增加
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward addModel(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("addModels");
		Model model = this.getModel(form);
		model.setId(RandomKey.generateID_Long_19().toString());
		model.setCreateDate(new Date());

		this.modelService.insertObject(model);

		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 删除
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward deleteModel(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("deleteModels");
		ModelForm modelForm = (ModelForm) form;
		this.modelService.deleteModels(modelForm);
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 到达更新页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toUpdateModel(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("toUpdateModels");
		ModelForm modelForm = (ModelForm) form;
		model = (Model) this.modelService.searchModel(
				modelForm).get(0);
		request.setAttribute(Constants.DEPARTMENT, model);
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 更新
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward updateModel(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("updateModels");
		Model model = this.getModel(form);
		model.setCreateDate(new Date());
		this.modelService.updateObject(model);

		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 查看页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward viewModel(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("viewModel");
		ModelForm modelForm = (ModelForm) form;
		model = (Model) this.modelService.searchModel(
				modelForm).get(0);
		request.setAttribute(Constants.DEPARTMENT, model);
		return mapping.findForward(Constants.SUCCESS);
	}

	// get--set
	/**
	 * @return the ModelService
	 */
	public ModelService getModelService() {
		return this.modelService;
	}

	/**
	 * @param ModelService
	 *            the ModelService to set
	 */
	public void setModelService(ModelService modelService) {
		this.modelService = modelService;
	}

	/**
	 * 获取Model实体
	 * 
	 * @param ActionForm
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 * @return the Model
	 */
	public Model getModel(ActionForm form)
			throws IllegalAccessException, InvocationTargetException {
		ModelForm modelForm = (ModelForm) form;
		model = new Model();
		BeanUtils.copyProperties(model, modelForm);
		return model;
	}

	public void setModel(Model model) {
		this.model = model;
	}

	public ActionForward creatTreeSelect(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		response.setContentType("text/text;charset=UTF-8");
		// System.out.println(request.getParameter("pid"));
		ModelForm modelForm = new ModelForm();
		modelForm.setFid(request.getParameter("pid"));
		String vid = request.getParameter("treeid");
		int tjs = Integer.parseInt(request.getParameter("js"));
		Long pid = Long.parseLong(request.getParameter("pid"));
		List<Model> accList = modelService
				.searchModel(modelForm);
		String html = creatTreeSelect(accList, vid, tjs);
		java.io.PrintWriter out = response.getWriter();
		out.print(html);
		out.flush();
		out.close();
		return null;
	}

	public String creatTreeSelect(List<Model> accList, String vid, int js) {
		String html = "";
		for (int i = 0; i < accList.size(); i++) {
			Model model = (Model) accList.get(i);
			for (int j = 0; j < js; j++) {
				html = html + "<img src='images/line.gif'>";
			}
			html = html + "<img src='images/plus.gif'  id='imgt" + vid + js + i
					+ "'  onclick=\"ajaxProcessBase('" + vid + js + i + "' ,"
					+ (js + 1) + ",'" + model.getId() + "');\" >"
					+ "<img src='images/close1.gif' id='img" + vid + js + i
					+ "' onclick=\"ajaxProcessBase('" + vid + js + i + "' ,"
					+ (js + 1) + ",'" + model.getId()
					+ "');\" style=\"cursor:hand\">&nbsp;";

			html = html + "<a onclick=\"selectTree('" + model.getId()
					+ "','" + model.getName()
					+ "')\" style=\"cursor:hand\">" + model.getName()
					+ "</a>";
			html = html + "<div id='" + vid + js + i
					+ "' ><span class='f1'>.</span></div>";
		}
		return html;
	}

}
